﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibrarySystem
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getBookList();
        }

        private void getBookList()
        {
            Global.BrowseBookList = new List<BusinessLayer.Book>();
            Global.BrowseBookList = BusinessLayer.Book.getAll();
            Global.BrowseBookList = Global.BrowseBookList.OrderBy(o => o.Title).ToList();
            //Global.BrowseBookList = Global.BrowseBookList.GroupBy(o => o.Title).Select(o => o.First()).ToList();            
            bookListView.DataSource = Global.BrowseBookList;
            bookListView.DataBind();     
   
        }
        private void getAuthorList()
        {
            Global.BrowseAuthorList = new List<BusinessLayer.Author>();
            Global.BrowseAuthorList = BusinessLayer.Author.GetAll();
            Global.BrowseAuthorList = Global.BrowseAuthorList.OrderBy(o => o.LastName).ToList();
            authorListView.DataSource = Global.BrowseAuthorList;
            authorListView.DataBind(); 
        }
        

        protected void listItem_ClickedBook(object sender, CommandEventArgs e)
        {
            string itemISBN = e.CommandName.ToString();
            Response.Redirect("bookDetailPage.aspx?isbn=" + itemISBN);
        }

        protected void listItem_ClickedAuthor(object sender, CommandEventArgs e)
        {
            string itemAid = e.CommandName.ToString();
            Response.Redirect("authorDetailPage.aspx?aid=" + itemAid);
        }

        protected void DataPager_PreRenderBook(object sender, EventArgs e)
        {
            bookListView.DataSource = Global.BrowseBookList;
            bookListView.DataBind();
        }
        protected void DataPager_PreRenderAuthor(object sender, EventArgs e)
        {
            authorListView.DataSource = Global.BrowseAuthorList;
            authorListView.DataBind();
        }

        protected void bookListView_DataBound(object sender, EventArgs e)
        {
            DataPager1.Visible = (DataPager1.PageSize < DataPager1.TotalRowCount);
            DataPager2.Visible = (DataPager2.PageSize < DataPager2.TotalRowCount);
        }
        protected void authorListView_DataBound(object sender, EventArgs e)
        {
            DataPager3.Visible = (DataPager3.PageSize < DataPager3.TotalRowCount);
            DataPager4.Visible = (DataPager4.PageSize < DataPager4.TotalRowCount);
        }

        protected void browseBooksLink_Click(object sender, EventArgs e)
        {
            listBook.Visible = true;
            listAuthor.Visible = false;
            getBookList();
        }

        protected void browseAuthorLink_Click(object sender, EventArgs e)
        {
            listBook.Visible = false;
            listAuthor.Visible = true;
            getAuthorList();
        }
    }
}